/*
大厅中的战绩界面，显示玩家历史玩过的游戏流水(不包括当庄模式的游戏记录)
*/
var confige = require("confige");
cc.Class({
    extends: cc.Component,

    properties: {
        history_item:{
            default:null,
            type:cc.Node
        },
        history_itemBox:{
            default:null,
            type:cc.Node
        },
        isInit:false,
    },

    onLoad: function () {

    },

    onInit:function(){
        this.historyContent = this.node.getChildByName("historyView").getChildByName("view").getChildByName("content");
        this.historyItemBeginY = -10;
        this.historyItemOffsetY1 = -165;
        this.historyItemOffsetY2 = -245;
        this.historyItemOffsetY3 = -325;

        this.historyItemBoxPosList = [{x:-253,y:-85},{x:0,y:-85},{x:253,y:-85},
                                      {x:-253,y:-165},{x:0,y:-165},{x:253,y:-165},
                                      {x:-253,y:-245},{x:0,y:-245},{x:253,y:-245}];
        this.historyItemList = {};
        this.isInit = true;
    },

    updateHistory:function(){
        for(var i in this.historyItemList)
        {
            this.historyItemList[i].destroy();
        }
        this.historyItemList = {};

        var newContentHeight = 0;
        var historyItemCount = 0;
        var newHistoryItemY = this.historyItemBeginY;
        for(var i in confige.curHistory.List)
        {
            historyItemCount++;

            var curRoomData = confige.curHistory.List[i];
            console.log("curRoomData======");
            console.log(curRoomData);

            var newHisPlayerCount = 0;
            for(var j in curRoomData.player)
                newHisPlayerCount ++;
            var newHistoryItem = cc.instantiate(this.history_item);
            newHistoryItem.y = newHistoryItemY;
            this.historyItemList[i] = newHistoryItem;
            this.historyContent.addChild(newHistoryItem);
            newHistoryItem.active = true;
            var boxList = newHistoryItem.getChildByName("boxList");
            var mainBg = newHistoryItem.getChildByName("mainBg");

            if(newHisPlayerCount <= 3){
                newHistoryItemY += this.historyItemOffsetY1;
                mainBg.height = -this.historyItemOffsetY1;
            }
            if(newHisPlayerCount > 3 &&newHisPlayerCount <= 6){
                newHistoryItemY += this.historyItemOffsetY2;
                mainBg.height = -this.historyItemOffsetY2;
            }
            if(newHisPlayerCount > 6){
                newHistoryItemY += this.historyItemOffsetY3;
                mainBg.height = -this.historyItemOffsetY3;
            }
            newHistoryItemY += -15;

            var hisIndex = newHistoryItem.getChildByName("index").getComponent("cc.Label");
            var roomID = newHistoryItem.getChildByName("roomID").getComponent("cc.Label");
            var date = newHistoryItem.getChildByName("date").getComponent("cc.Label");
            hisIndex.string = (parseInt(i)+1);
            roomID.string = "房间号：" + curRoomData.roomId;
            var curDate = curRoomData.date.year+"/";
            
            if(curRoomData.date.month<9)
            {
                curDate = curDate + "0" + (curRoomData.date.month+1) + "/";
            }else{
                curDate = curDate + (curRoomData.date.month+1) + "/";
            }
            if(curRoomData.date.day<10)
            {
                curDate = curDate + "0" + curRoomData.date.day + "  ";
            }else{
                curDate = curDate + curRoomData.date.day + "  ";
            }
            if(curRoomData.date.hours<10)
            {
                curDate = curDate + "0" + curRoomData.date.hours + ":";
            }else{
                curDate = curDate + curRoomData.date.hours + ":";
            }
            if(curRoomData.date.minute<10)
            {
                curDate = curDate + "0" + curRoomData.date.minute + ":";
            }else{
                curDate = curDate + curRoomData.date.minute + ":";
            }
            if(curRoomData.date.second<10)
            {
                curDate = curDate + "0" + curRoomData.date.second;
            }else{
                curDate = curDate + curRoomData.date.second;
            }
            date.string = "时间："+curDate;

            for(var j in curRoomData.player)
            {
                var newPlayerInfo = cc.instantiate(this.history_itemBox);
                newPlayerInfo.x = this.historyItemBoxPosList[j].x;
                newPlayerInfo.y = this.historyItemBoxPosList[j].y;
                boxList.addChild(newPlayerInfo);
                newPlayerInfo.active = true;
                var curPlayerData = curRoomData.player[j];
                var curNameL = newPlayerInfo.getChildByName("nick").getComponent("cc.Label");
                var curScoreL = newPlayerInfo.getChildByName("score").getComponent("cc.Label");
                curNameL.string = curPlayerData.name;
                curScoreL.string = Math.round(parseInt(curPlayerData.score))
            }
        }
        if(newContentHeight < 500)
            newContentHeight = 500;
        this.historyContent.height = -newHistoryItemY + 100;
    },

    showLayer:function(){
        if(this.isInit == false)
            this.onInit();
        this.node.active = true;
        this.updateHistory();
    },

    hideLayer:function(){
        this.node.active = false;
    },
});
